package com.example.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;

import com.example.util.StringUtil;

/**
 * 解决ajax跨域问题过滤器
 * @author Administrator
 *
 */

public class HeaderFilter implements Filter {

	@Override
	public void destroy() {
		System.out.println("销毁过滤器");
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain)
			throws IOException, ServletException {
	    	HttpServletRequest request = (HttpServletRequest) req; 
		    HttpServletResponse response = (HttpServletResponse) resp; 
		    response.setHeader("Access-Control-Allow-Origin", "*"); //解决跨域访问报错   
		    response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");   
		    response.setHeader("Access-Control-Max-Age", "3600"); //设置过期时间   
		    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, client_id, uuid, Authorization");   
		    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // 支持HTTP 1.1.   
		    response.setHeader("Pragma", "no-cache"); // 支持HTTP 1.0. response.setHeader("Expires", "0");   
		    filterChain.doFilter(request, resp);  
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("初始化过滤器");
		
	}

}
